package com.ruoyi.develop.produceRecordDetail.controller;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.lvRuku.service.ILvRukuService;
import com.ruoyi.develop.produceRecord.domain.ProduceRecord;
import com.ruoyi.develop.produceRecord.service.IProduceRecordService;
import com.ruoyi.export.service.IExportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.produceRecordDetail.domain.ProduceRecordDetail;
import com.ruoyi.develop.produceRecordDetail.service.IProduceRecordDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产记录明细Controller
 * @author lmm
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/produceRecordDetail/produceRecordDetail")
public class ProduceRecordDetailController extends BaseController {
    @Autowired
    private IProduceRecordDetailService produceRecordDetailService;
    @Autowired
    private IProduceRecordService produceRecordService;
    @Autowired
    private ILvRukuService lvRukuService;
    @Autowired
    private IExportService exportService;

    /**
     * 查询生产记录明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecordDetail:query')")
    @GetMapping("/list")
    public TableDataInfo list(ProduceRecordDetail entity) {
        startPage();
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出生产记录明细列表
     */
    @Log(title = "生产记录明细列表导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProduceRecordDetail entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String date = DateUtils.getTime();//默认当天
        startPage();
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("fundCallback", DictUtils.getDictLabel(BusinessConstants.FUND_CALLBACK, e.getFundCallback()));
            LocalDateTime localDateTime = (LocalDateTime) e.getParams().get("produceDate");
            Date produceDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
            map.put("produceDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, produceDate));
            e.setParams(map);
        });
        params.put("list", list);
        params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "生产产品信息.xlsx";
        }
        String templateName = "生产产品信息.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }

    /**
     * 获取生产记录明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecordDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(produceRecordDetailService.selectById(id));
    }

    /**
     * 新增生产记录明细
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecordDetail:add')")
    @Log(title = "生产记录明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProduceRecordDetail entity) {
        return toAjax(produceRecordDetailService.insert(entity));
    }

    /**
     * 修改生产记录明细
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecordDetail:edit')")
    @Log(title = "生产记录明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ProduceRecordDetail entity) {
        return toAjax(produceRecordDetailService.update(entity));
    }

    /**
     * 删除生产记录明细
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecordDetail:remove')")
    @Log(title = "生产记录明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(produceRecordDetailService.deleteByIds(ids));
    }

    /** * 库管退回 */
    @PutMapping(value = "/storeReject")
    public AjaxResult storeReject(@RequestBody ProduceRecordDetail entity) {
        entity.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT);
        produceRecordDetailService.update(entity);
        return AjaxResult.success("库管退回成功");
    }

    /** * 库管入库 退回 */
    @PutMapping(value = "/storeReturn")
    public AjaxResult storeReturn(@RequestBody ProduceRecordDetail entity) {
        entity.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT);
        produceRecordDetailService.update(entity);
        // 库管通过审核 入库
        lvRukuService.chukuByProduceDetail(entity);
        return AjaxResult.success("审核通过");
    }

    /** * 库管入库 */
    @GetMapping(value = "/storeIn/{id}")
    public AjaxResult storeIn(@PathVariable("id") String id) {
        ProduceRecordDetail detail = produceRecordDetailService.selectById(id);
        ProduceRecord produceRecord = produceRecordService.selectById(detail.getProduceId());
        if(produceRecord!=null && StringUtils.isBlank(produceRecord.getRukuId())){
            return AjaxResult.error("请先入库生产单！");
        }
        detail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_PASS);
        produceRecordDetailService.update(detail);
        // 库管通过审核 入库
        lvRukuService.rukuByProduceDetail(detail,produceRecord);
        return AjaxResult.success("审核通过");
    }











}
